﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Management;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;

namespace ShareTools
{
    
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
    class PLAYLIST
    {
        public string[,] mediaInfo = new string[10,2];
        public string playStartTime;
        
    }
    /// <summary>
    /// 远程文件夹操作类,必须在工程中添加SYSTEM.MANAGEMENT引用
    ///MapDriveECS将远程文件夹映射到本地，参数：远程地址，用户名，密码
    ///TransferMedia用来复制文件到目标文件夹，参数：源地址，目标地址，地址要包括文件名
    /// </summary>
    class REMOTEDIRECTORY
    {
        /// <summary>
        /// 读取远程文件，参数为文件路径，包括文件名，返回是字符串数组
        /// </summary>
        public string[] ReadRemoteFile(string filePath)
        {
            string[] fileStr = new string[10] {"","", "", "", "", "", "", "", "", null};

            FileStream fs1 = new FileStream(filePath ,FileMode.Open ,FileAccess .Read);
            StreamReader sr1 = new StreamReader(fs1);
            int count = 0;
            while (fileStr[count ]!= null)
            {
                fileStr[count] = sr1.ReadLine();
                count++;
            }
            return fileStr;
        }
        /// <summary>
        /// 用来向远程文件夹写入txtfile
        /// </summary>
        public void UploadTxtFile(string txtPath, string[] writeBuffer)
        {
            if (File.Exists(txtPath))
            {
                File.Delete(txtPath);
            }
            FileStream fs1 = new FileStream(txtPath, FileMode.Create, FileAccess.Write);
            StreamWriter sw1 = new StreamWriter(fs1);
            int count = 0;
            foreach (string i in writeBuffer)
            {
                sw1.WriteLine(writeBuffer[count]);
                count++;
            }
            sw1.Close();
            fs1.Close();
        }
        /// <summary>
        /// 用来映射远程文件夹
        /// </summary>
        /// <param name="remotePath"></param>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        public void MapDriveECS(string remotePath, string userName, string passWord)
        {
            //String cmdString = "net use \\\\10.1.1.254\\content" + " " + "HDMA-4000" + " " + "hdp";
            string cmdString = "net use " + remotePath + " " + userName + " " + passWord;
            ManagementClass processClass = new ManagementClass("Win32_Process");
            object[] methodArgs = { cmdString, null, null, 0 };
            object result = processClass.InvokeMethod("Create", methodArgs);
        }
        //源地址和目的地址必须包括文件名
        public void TransferMedia(string sourcePath, string destinationPath)
        {
            File.Copy(sourcePath, destinationPath);
        }
        //读取远程文件夹信息，返回字符串
        public string[] ReadRemoteDirectory(string filePath)
        {
            string[] directoryInfo = Directory.GetFiles(filePath);
            return directoryInfo;
        }
    }


    /// <summary>
    /// 根据exepath和mediapath，取出视频的所有信息
    /// </summary>
    class FFMPEG
    {
        public string mediaInfo = null;

        public void callFFMPEG(string exePath, string mediaPath)
        {
            Process p = new Process();//建立外部调用线程
            p.StartInfo.FileName = exePath;//要调用外部程序的绝对路径
            //p.StartInfo .FileName = exePath +"resource\\"+"ffmpeg.exe";
            //p.StartInfo.Arguments = "-i d:\\xxxx.wmv";//参数(这里就是FFMPEG的参数了)
            p.StartInfo.Arguments = "-i " + mediaPath;
            p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
            p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...这是我耗费了2个多月得出来的经验...mencoder就是用standardOutput来捕获的)
            p.StartInfo.CreateNoWindow = false;//不创建进程窗口
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
            p.Start();//启动线程
            p.BeginErrorReadLine();//开始异步读取
            p.WaitForExit();//阻塞等待进程结束
            p.Close();//关闭进程
            p.Dispose();//释放资源
        }
        private void Output(object sendProcess, DataReceivedEventArgs output)
        {
            if (!String.IsNullOrEmpty(output.Data))
            {
                mediaInfo = mediaInfo + output.Data + "\r\n";
            }
        }
    }
    /// <summary>
    /// 字符串处理类
    /// </summary>
    class StringDealWith
    {
        /// <summary>
        /// 在输入的字符串里面，找到相似的字符串的位置，并且向后取length长的字符串，返回
        /// </summary>
        /// <param name="stringData"></param>
        /// <param name="compareString"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public string GetSubString(string stringData, string compareString, int length)
        {
            string returnString = null;
            int position = stringData.IndexOf(compareString);
            returnString = stringData.Substring(position, length);
            return returnString;
        }
    }
    /// <summary>
    /// 用来在首页上添加log
    /// </summary>
    class LOG
    {
        public void WriteToLog(string data)
        {
            Form1 formlog = new Form1();
            formlog.textBox1.Text= data ;
            formlog.textBox1.Visible = false;
        }
    }
}
